package test.yzx.pproject.view;


import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.IntRange;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class HomeNumberLayout extends LinearLayout{

    public HomeNumberLayout(Context context) {
        this(context, null);
    }
    public HomeNumberLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public HomeNumberLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init(){
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        addView(new HomeSingleNumberView(getContext()));
        addView(new HomeSingleNumberView(getContext()));
        addView(new HomeSingleNumberView(getContext()));
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            ViewGroup.LayoutParams lp = getChildAt(i).getLayoutParams();
            lp.height = -1;
            lp.width = -2;
        }
        super.onLayout(changed, l, t, r, b);
    }



    /* 设置显示数字 */
    private void setNumber(boolean anim, String... v){
        for (int i = 0; i < getChildCount(); i++) {
            HomeSingleNumberView child = (HomeSingleNumberView) getChildAt(i);
            if(v[i] == null) {
                child.setNumber(0);
                child.setVisibility(View.GONE);
                continue;
            }
            child.setVisibility(View.VISIBLE);
            if(anim)
                child.animTo(Integer.parseInt(v[i]), duration);
            else
                child.setNumber(Integer.parseInt(v[i]));
        }
    }
    /* 分割数字 */
    private String[] splitNumber(int number){
        if(number==100)
            return new String[]{"1","0","0"};
        String strNum = number+"";
        if(number<10)
            return new String[]{null, null, strNum};
        return new String[]{null, strNum.charAt(0)+"", strNum.charAt(1)+""};
    }



    private int duration = 1100;

    /** 设置动画时长 */
    public void setDuration(int duration) {
        this.duration = duration;
    }
    /** 设置文字颜色 */
    public void setTextColor(@ColorInt int textColor) {
        for (int i = 0; i < getChildCount(); i++) {
            HomeSingleNumberView child = (HomeSingleNumberView) getChildAt(i);
            child.setTextColor(textColor);
        }
    }
    /** text size : dp */
    public void setTextSize(int dp){
        for (int i = 0; i < getChildCount(); i++) {
            HomeSingleNumberView child = (HomeSingleNumberView) getChildAt(i);
            child.setTextSize(dp);
        }
        requestLayout();
    }
    /** 设置显示数字 */
    public void setNumber(@IntRange(from = 0, to = 100) final int value, final boolean anim){
        if(value < 0 || value > 100)
            throw new IllegalStateException("value from 0 to 100");
        post(new Runnable() {
            public void run() {
                setNumber(anim, splitNumber(value));
            }
        });
    }

}
